//
//  WelcomeViewController.swift
//  weibo
//
//  Created by 谭仁川 on 15/12/1.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class WelcomeViewController: UIViewController {
    override func loadView() {
        view = backImage
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
       iconImage.sd_setImageWithURL(UserAccountViewModel().headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    
    private func startAnimation(){
        let offset = -screenH + 180
        iconImage.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(offset)
        }
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
        }
    }
    
    private func setupUI(){
        view.addSubview(iconImage)
        view.addSubview(welcomeLabel)
    
    
        iconImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.centerX.equalTo(iconImage.snp_centerX)
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(16)
            make.centerX.equalTo(iconImage.snp_centerX)
            
        }
        iconImage.layer.cornerRadius = 90
        
        iconImage.layer.masksToBounds = true
    
    }
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconImage: UIImageView = UIImageView()
    
    private lazy var welcomeLabel: UILabel = UILabel(title: (UserAccountViewModel().userName ?? "") + " 欢迎回来", color: UIColor.darkGrayColor(), fontSize: 19)
}
